/**
 * @file
 * @author LiQun
 * @date 2021/11/28
 */
const execa = require('execa');
const shelljs = require('shelljs');
var spawn = require('child_process').spawn;
const fs = require('fs-extra');

const {getActiveProjectInfo} = require('./utils');

async function checkInstall() {
  // let result = await execa('git');
  // console.log('result', result);
  return new Promise((resolve, reject) => {
    execa("git", ['version']).then(result => {
      resolve(true)
    }).catch(err => {
      resolve(false)
    });
  })
}

async function clone(gitPath, branch, projectPath) {
  return new Promise((resolve, reject) => {
    // const {stdout} = execa("git", ['clone', gitPath, '/Users/gemini/Desktop/demo']).then(result => {
    //   // console.log(result.stdout)
    //   // result.stdout.on('data',function (chunk) {
    //   //   console.log(chunk)
    //   // })
    //   // result.stdout.on('end',function (chunk) {
    //   //   resolve(true)
    //   // })
    // }).catch(err => {
    //   console.log('err', err);
    //   resolve(false)
    // });
    // stdout.on('data',function (chunk) {
    //   console.log(chunk)
    // })
    // stdout.on('end',function (chunk) {
    //   resolve(true)
    // })

    // execa("git", ['clone', gitPath, '/Users/gemini/Desktop/demo']).stdout.on("data", (chunk) => {
    //   console.log('chunk', chunk);
    // })
    // console.log('stdout', stdout);

    // var spawn = require('child_process').spawn,
    //   ls    = spawn('git', ['clone', gitPath]);
    //
    // ls.stdout.on('data', function (data) {
    //   console.log('stdout: ' + data.toString());
    // });
    //
    // ls.stderr.on('data', function (data) {
    //   console.log('stderr: ' + data);
    // });
    //
    // ls.on('exit', function (code) {
    //   console.log('child process exited with code ' + code.toString());
    // });
    // shelljs.exec(`git clone ${gitPath} ${projectPath}`, {async: true}).stdout('data', (chunk) => console.log(chunk))

    // const child = spawn('git', ['clone', gitPath, projectPath], {stdio: 'inherit'});
    // console.log('11111', 11111);
    // 提取git项目名
    const regexp = /(?<=\/)[^\/]+(?=\.git)/;
    const projectName = regexp.exec(gitPath)[0];
    fs.removeSync(projectPath + '/' + projectName);
    console.log('正在拉取代码...')
    execa("git", ['clone', '-b', branch, gitPath,], {
      cwd: projectPath,
      async: true
    }).then(result => {
      resolve(true);
    }).catch(err => {
      console.log(err.stderr);
      resolve(false)
    });
    // console.log('stdout', stdout);

    // child.stdout.on('data', (data) => {
    //   console.log(`stdout: ${data}`);
    // });
    //
    // child.stderr.on('data', (data) => {
    //   console.log(`stderr: ${data}`);
    // });
    //
    // child.stdout.on('close', (code) => {
    //   resolve(true);
    // });
  })
}

// clone('http://49.235.63.253:20003/zhongliang/link-zl-taro-mp.git', '/Users/gemini/Desktop/demo');
// clone('https://github.com/isnl/wechat-robot.git')

function pull() {
  return new Promise((resolve, reject) => {
    console.log('正在拉取最新代码...');
    let lastProjectInfo = getActiveProjectInfo();
    if (!lastProjectInfo) {
      console.log('未获取到项目信息!');
      return;
    }
    const projectPath = lastProjectInfo.projectPath
    execa("git", ['pull'], {
      cwd: projectPath,
      async: true
    }).then(result => {
      resolve(true);
    }).catch(err => {
      console.log(err.stderr);
      resolve(false)
    });
  })
}

module.exports.checkInstall = checkInstall;

module.exports.clone = clone;

module.exports.pull = pull;
